//
//  HomeController.swift
//  BQWB
//
//  Created by laibingqiang on 16/10/22.
//  Copyright © 2016年 laibingqiang. All rights reserved.
//

import UIKit

class HomeController: BQBaseViewController {

    
    var statusListViewModel = HomeStatusListViewModel()
    override func viewDidLoad() {
        super.viewDidLoad()

        if !BQOAuthViewModel.shared.isLogin{
            
            visitorV?.setupSubViews(text: nil, img: nil)
            return
        }
        setupUI()
        
        getData()
    }
    
    @objc private func saoYiSaoClink(){
        
        print("跳转扫一扫")
        
    }
    
    @objc private func btn2Clink(){
        
        print("btn2")
        
        let vc:TempController = TempController()
        
        vc.hidesBottomBarWhenPushed = true
        
        self.navigationController?.pushViewController(vc, animated: false)
    }
    
    private func setupUI(){
        
        setupNav()
        
        setupTabView()
        //创建tip动画
        let nva:BQNavController = self.navigationController as! BQNavController
        nva.view.insertSubview(tip_lab, belowSubview: nva.navigationBar)
    }
    //tip动画方法
    fileprivate func tip_labanimating(text:String){
        //如果正在做动画，就return
        if tip_lab.isHidden == false{
            return
        }
        tip_lab.text = text
        UIView.animate(withDuration: 1.0, animations: {
            self.tip_lab.isHidden = false
            self.tip_lab.transform = CGAffineTransform(translationX: 0, y: 35)
            }) { (_) in
                
                UIView.animate(withDuration: 1.0, animations: { 
                    self.tip_lab.transform = CGAffineTransform.identity
                    }, completion: { (_) in
                        self.tip_lab.isHidden = true
                })
        }
        
    }
    
    private func setupNav(){
        
        self.navigationItem.title = "主页"
        
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(target: self, action: #selector(saoYiSaoClink), imgName: "navigationbar_pop", backImg: nil, title: nil)
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(target: self, action: #selector(btn2Clink), imgName: "navigationbar_friendsearch", backImg: nil, title: nil)
        
    }
    
    private func setupTabView(){
        
        tableView.dataSource = self
        tableView.delegate = self
        tableView.estimatedRowHeight = 300
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.separatorStyle = .none
        tableView.register(HomeCell.self, forCellReuseIdentifier: "cell")
        
        tableView.tableFooterView = shangLa
        
        tableView.addSubview(xiaLa)
        
        xiaLa.addTarget(self, action: #selector(getData), for: UIControlEvents.valueChanged)
        
        
    }

    //tip label
    fileprivate var tip_lab:UILabel = {
        
        let label = UILabel()
        label.frame = CGRect(x: 0, y: 29, width: UIScreen.main.bounds.width, height: 35)
        label.backgroundColor = UIColor.green
        label.textColor = UIColor.red
        label.font = UIFont.systemFont(ofSize: 12)
        label.textAlignment = .center
        label.text = ""
        label.isHidden = true
        return label
    }()
    //懒加载上拉加载更多
    fileprivate lazy var shangLa:UIActivityIndicatorView = {
        let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
        view.color = UIColor.purple
        return view
    }()
    //下拉刷新
    //fileprivate lazy var xiaLa:UIRefreshControl = UIRefreshControl()
    fileprivate lazy var xiaLa:BQRefreshControl = BQRefreshControl()

}
//tableView数据源方法
extension HomeController:UITableViewDataSource,UITableViewDelegate{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statusListViewModel.homeDataArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! HomeCell
        
        cell.statusViewModel = statusListViewModel.homeDataArr[indexPath.row]
        cell.selectionStyle = .none
        return cell
    }
    //上拉加载更多
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if indexPath.row == (statusListViewModel.homeDataArr.count) - 1{
            shangLa.startAnimating()
            getData()
        }
        
    }
    
}

//请求数据相关

extension HomeController{
    
    func getData(){
        
        statusListViewModel.getHomeDataInfo(isAnimating:shangLa.isAnimating) { (isSuccess,str) in
            
            if isSuccess == true{
                
                self.tableView.reloadData()
            }
            else
            {
                print("请求失败")
                
            }
            if !self.shangLa.isAnimating{
                
                self.tip_labanimating(text: str)
            }
            self.refreshing()
        }
    }
    
    func refreshing(){
        
        self.shangLa.stopAnimating()
        self.xiaLa.endRefreshing()
    }
    
    
}
